
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id0: '0',
        id1: '1',
        id2: '2',
        id3: '3',
        id4: '4',
        id5: '5',
        id6: '6',
        id7: '7',
        id8: '8',
        id9: '9',
        id10: '←',
        id11: 'AC',

        formula: '',
        screenData: '',
        subject: [],

        pause: false,  //控制用户连续点击

        record: [],


        timer: '',  //定时器名字
        timeleft: 0,//显示的剩余时间
        countDownNum: 60,//倒计时初始值
        flag: true,  //是否有计时器

        i: 1,
        sum: 30,
        right: 0,
        wrong: 0,

        judge: 3  //1代表正确，2代表错误，3代表还未提交答案
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        this.setData({ i: 1 });
        this.setData({ judge: 3 });
        this.setData({ record: [] });   //提交结果记录
        this.setData({ flag: true });   //计时器
        this.setData({ pause: false }); //连续点击
        this.setData({ right: 0 });
        this.setData({ wrong: 0 });
        this.setData({ countDownNum: 20 });
        this.setData({ sum: 30 });

        var grade = wx.getStorageSync('grade');

        if (grade == '自定义') {
            var configs = wx.getStorageSync('configs');
            if (configs.config1.value == '1')
                this.setData({ countDownNum: 5 });  //判断时间设置选项来给时间赋值
            else if (configs.config1.value == '2')
                this.setData({ countDownNum: 10 });
            else if (configs.config1.value == '3')
                this.setData({ countDownNum: 20 });
            else
                this.setData({ flag: false });

            if (configs.config2.value == '20')
                this.setData({ sum: 20 });        //判断题量设置选项来给题量赋值
            else if (configs.config2.value == '30')
                this.setData({ sum: 30 });
            else
                this.setData({ sum: 40 });


            var num = this.data.sum;
            var step = configs.config3.value;
            var range = configs.config4.value;
            var calcu_type = configs.config5.value;
            var method = require('../../utils/subject.js');
            var subject = method.byGrade(grade, num, Number(step), Number(range), Number(calcu_type));
            this.setData({ subject: subject });
            //调函数获得大数组
            this.setData({ formula: this.data.subject[this.data.i - 1][0] });

            //根据情况启动定时器
            if (this.data.flag == true)
                this.countDown();
        }
        else {
            var method = require('../../utils/subject.js');
            var subject = method.byGrade(grade);
            //调函数获得大数组
            this.setData({ subject: subject });
            this.setData({ formula: this.data.subject[this.data.i - 1][0] });
            this.countDown();
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        if (this.data.flag == true)//防止用户点左上角返回
            clearInterval(this.data.timer);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    clickButton: function (event) {
        var id = event.target.id;
        if (id == this.data.id10) { //退格
            var data = this.data.screenData;
            if (data == "") {
                return;
            }
            data = data.substring(0, data.length - 1);
            this.setData({ screenData: data });
        }
        else if (id == this.data.id11) {//清屏
            this.setData({ screenData: "" });
        }
        else {
            var sd = this.data.screenData;
            if (sd.length <= 5) {
                var data;
                if (sd == "0") {
                    data = id;
                }
                else {
                    data = sd + id;
                }
                this.setData({ screenData: data });
            }
        }
    },

    countDown: function () {
        var countDownNum = this.data.countDownNum;//获取倒计时初始值
        this.setData({ timeleft: countDownNum });
        var that = this;
        //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
        that.data.timer = setInterval(function () {//这里把setInterval赋值给变量名为timer的变量
            if (countDownNum == 0) {
                //计时器是始终一直在走的，如果时间为0，那么就要关掉定时器,不然耗性能
                that.confirm();
            }
            else {
                countDownNum--;
                that.setData({ timeleft: countDownNum });
            }
        }, 1000)
    },

    confirm: function () {
        if (this.data.pause == false) {
            this.setData({ pause: true });
            if (this.data.flag == true)
                clearInterval(this.data.timer);
            //判断screendata与答案是否相等
            //根据情况修改jugde的值
            if (this.data.screenData == String(this.data.subject[this.data.i - 1][1])) {
                var right = this.data.right + 1;
                this.setData({ right: right });
                this.setData({ judge: 1 });
            }
            else {
                var wrong = this.data.wrong + 1;
                this.setData({ wrong: wrong });
                this.setData({ judge: 2 });
                const db = wx.cloud.database();
                const history = db.collection('history');
                db.collection('history').add({
                    data: {
                        formula: this.data.subject[this.data.i - 1][0],
                        answer: this.data.subject[this.data.i - 1][1],
                    }
                }).then(res => {
                    console.log(res)
                }).catch(err => {
                    console.log(err)
                })
            }
            //记录对错，正确答案，formula+screenData存到record去
            var record = [];
            var str = this.data.formula;
            str = str.replace(/\[/g, '[ ');
            str = str.replace(/\]/g, this.data.screenData + ' ]');
            record.push(str);
            record.push(this.data.judge);
            record.push(this.data.subject[this.data.i - 1][1]);
            this.data.record.push(record);

            var that = this;
            setTimeout(function () {
                var i = that.data.i + 1;
                if (i == that.data.sum + 1) {//跳到结束页面去
                    wx.setStorageSync('right', that.data.right);
                    wx.setStorageSync('wrong', that.data.wrong);
                    wx.setStorageSync('record', that.data.record);
                    wx.redirectTo({
                        url: '../result/result',
                    })
                }
                else {
                    that.setData({ i: i });
                    that.setData({ judge: 3 });
                    that.setData({ screenData: "" });
                    if (that.data.flag == true)
                        that.countDown();  //重新设置计时器计时
                    //重新设置计算数的值
                    that.setData({ formula: that.data.subject[that.data.i - 1][0] });
                    that.setData({ pause: false });
                }
            }, 1000)
        }
    }
})